//
//  XJRegistSetInfoViewController.m
//  XinJiangMall
//
//  Created by Tzyang on 2018/4/8.
//  Copyright © 2018年 Tzyang. All rights reserved.
//

#import "XJRegistSetInfoViewController.h"
#import "NSString+LSMD5.h"

@interface XJRegistSetInfoViewController ()<XJRegisterViewDelegate,UIPickerViewDelegate,UINavigationControllerDelegate>
//{
//    UIImagePickerController *_picker
//}
@property(nonatomic, strong)UIImagePickerController *picker;

@end

@implementation XJRegistSetInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navTitle = @"注册新账号";
   // self.navButtonRight.hidden = YES;
   // self.navButtonLeft.hidden = NO;
    [self.view addSubview:self.setInfoView];
    self.picker = [[UIImagePickerController alloc] init];
    self.picker.allowsEditing = YES;
    
    
    self.picker.delegate = self;
//    Assigning retained object to unsafe property; object will be released after assignment
}

- (XJRegistSetInfo *)setInfoView{
    
    if (!_setInfoView) {
        XJRegistSetInfo * setInfoView = [[XJRegistSetInfo alloc]initWithFrame:CGRectMake(0, XJTopNavH, KWidth, KHeight-XJTopNavH)];
        setInfoView.delegate = self;
        self.setInfoView = setInfoView;
        //[self.view addSubview:setInfoView];
    }
    return _setInfoView;
}

//- (void)viewWillAppear:(BOOL)animated{
//    [super viewWillAppear:animated];
//  //  [self.navigationController setNavigationBarHidden:NO animated:YES];
//    self.tabBarController.tabBar.hidden=YES;
//}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.view endEditing:YES];
    
}


#pragma mark -  UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {
    
    NSLog(@"%@",info);
    
    // 选择的图片
    UIImage *img = info[UIImagePickerControllerOriginalImage];
//    self.iv.image = img;
    self.setInfoView.iconImgV.image = img;
    
    // 将图片转换为data
//    NSData *imgData = UIImageJPEGRepresentation(img, 0.3);//压缩图片:(图片，压缩比例)
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    [self dismissViewControllerAnimated:YES completion:nil];
}
//

#pragma mark - XJRegisterViewDelegate
- (void)clickUpdateImage {
    XJLog(@"上传图片");

    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"提示" message:@"选择上传方式" preferredStyle:UIAlertControllerStyleActionSheet];
    
    UIAlertAction *xiangCe = [UIAlertAction actionWithTitle:@"从相册选择" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
        _picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        _picker.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:_picker.sourceType];
        [self presentViewController:_picker animated:YES completion:nil];
    }];
    
    UIAlertAction *paizhao = [UIAlertAction actionWithTitle:@"拍照上传" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
        _picker.sourceType = UIImagePickerControllerSourceTypeCamera;
        [self presentViewController:_picker animated:YES completion:nil];
    }];
    
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleCancel) handler:nil];
    
    [alertVC addAction:xiangCe];
    [alertVC addAction:paizhao];
    [alertVC addAction:cancel];
    
    [self presentViewController:alertVC animated:YES completion:nil];
}

- (void)clickRegistBtn {
    
    XJLog(@"进入新疆商城");
    
    if (![self checkoutInfoIsFullWith]) {
        XJLog(@"信息不完整/不合法");
        return;
    }
    
    XJLog(@"ok");

    NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
    fmt.dateFormat = @"yyyyMMddHHmmss";
    NSString *dateStr = [fmt stringFromDate:[NSDate date]];
    NSString *imgStr = [NSString stringWithFormat:@"%@.jpg",dateStr];

    NSString *nickname = self.setInfoView.setInfoNickTextField.text;
   
    NSString *sex = self.setInfoView.sexStr;
    NSString *repwd = self.setInfoView.setInfoRePasswdTextField.text;
    
    // 对密码进行MD5加密 并且 转小写
    NSString *md5Pwd = [NSString md5:repwd];
    md5Pwd = md5Pwd.lowercaseString;
    
    // 取出手机号好
    NSString *phone = [XJTool xj_localPhone];
    NSDictionary *dic = @{@"avatar":imgStr,
                          @"sex":sex,
                          @"nickname":nickname,
                          @"password":md5Pwd,
                          @"phone":phone
                          };
    
    [XJHttpTool requestRegisterParameter:dic successHandler:^(id responseObjc) {
        XJLog(@"注册成功:%@",responseObjc);
    } failureHandler:^(NSError *error) {
        [MBProgressHUD showError:[NSString stringWithFormat:@"%@",error.localizedDescription]];
    }];
}



- (BOOL)checkoutInfoIsFullWith{
    NSString *nickname = self.setInfoView.setInfoNickTextField.text;
    NSString *sex = self.setInfoView.sexStr;
    NSString *pwd = self.setInfoView.setInfoPasswdTextField.text;
    NSString *repwd = self.setInfoView.setInfoRePasswdTextField.text;
    
    if ([XJTool isEmptyStr:nickname]) {
        [MBProgressHUD showError:@"请输入昵称"];
        return NO;
    }
    if ([XJTool isEmptyStr:sex]) {
        [MBProgressHUD showError:@"请选择性别"];
        return NO;
    }
    
    if ([XJTool isEmptyStr:pwd]) {
        [MBProgressHUD showError:@"请输入密码"];
        return NO;
    }
    
    if ([XJTool isEmptyStr:repwd]) {
        [MBProgressHUD showError:@"请输确认密码"];
        return NO;
    }
    
    if (![pwd isEqualToString:repwd]) {
        [MBProgressHUD showError:@"两次密码不一致"];
        return NO;
    }
    
    return YES;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
